package constants.system;

import constants.Default;
import constants.session.Session;
import play.mvc.Http.Request;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;


/**
 * 会员对象，用于管理当前浏览者（游客或者CP）
 *
 * @author yangzhi@djcars.cn
 */
public class MemberShip {

    /**
     * 游客对象
     */
    private static MemberShip guest = new MemberShip(null,null, null,null);
    /**
     * 如果jobId 是1 则为管理人员
     */
    private MemberShip(String name,Long id,String sessionId,String userRoleIds) {
        this.name = name;
        this.id = id;
        this.sessionId = sessionId;
        this.userRoleIds = userRoleIds;
    }


    /**
     * 账号ID
     */
    private Long id;

    /**
     * 姓名
     */
    private String name;

    /**
     * 手机号码
     */
    private String mobile;

    /**
     * 登录服务端给的sessionId
     */
    private String sessionId;

    /**
     * 登录用户全部角色ids
     */
    private String userRoleIds;

    /**
     * 获取当前会员的账号名称
     */
    public String getName() {
        return this.name;
    }

    public Long getId() {
        return this.id;
    }

    public String getSessionId() {
        return this.sessionId;
    }

    public String getUserRoleIds() {
        return this.userRoleIds;
    }

    public String getMobile() {
        return this.mobile;
    }

    /**
     * 当前会员是否是游客
     */
    public Boolean isGuest() {
        return StringUtils.isEmpty(this.name);
    }

    /**
     * 退出
     */
    public void logout() {

        Session.current().remove(MemberShip.getSessionKey("Name"));
    }

    /**
     * 缓存key
     */
    private static String CURRENT_MEMBERSHIP_KEY = "CURRENT_MEMBERSHIP";

    /**
     * 转换当前会员的账号身份
     *
     * @param name 新的账号名
     */
    public static MemberShip change(Long id,String name,String sessionId,String userRoleIds) {
        MemberShip current = MemberShip.current();
        if (!current.isGuest()) {
            // 同一个用户
            if (current.getName().equals(name)) {
                return current;
            }
            // 退出，清空session数据
            current.logout();
        }
        if (!StringUtils.isEmpty(name)) {
            current = new MemberShip(name,id,sessionId,userRoleIds);
            Request.current().args.put(CURRENT_MEMBERSHIP_KEY, current);
            //生成dashboard的sessionId和会话缓存
            Session dashboardSession=Session.current();
            dashboardSession.add(MemberShip.getSessionKey("Id"),id);
            dashboardSession.add(MemberShip.getSessionKey("Name"),name);
            dashboardSession.add(MemberShip.getSessionKey("SessionId"),sessionId);
            dashboardSession.add(MemberShip.getSessionKey("UserRoleIds"),userRoleIds);
        } else {
            current = MemberShip.guest;
        }
        return current;
    }

    /**
     * 返回当前会员相关的session的数据键值
     *
     * @param key 数据键值
     */
    public static String getSessionKey(String key) {
        return "PT/MS/".concat(key.toUpperCase());
    }

    /**
     * 当前登录的会员
     *
     * @return 如果未登录，则返回MemberShip.guest对象
     */
    public static MemberShip current() {
        MemberShip current = (MemberShip) Request.current().args.get(CURRENT_MEMBERSHIP_KEY);
        if (current == null) {
            // 读取session的数据
            current = fromSession(Session.current(),null,null,null);
            if (current == null) {
                current = MemberShip.guest;
            }
            Request.current().args.put(CURRENT_MEMBERSHIP_KEY, current);
        }

        return current;
    }

    /**
     * 从session获取会员对象
     * @param session
     * @return
     */
    public static MemberShip fromSession(Session session,Long id,String sessionId,String userRoleIds){
        String nameKey = MemberShip.getSessionKey("Name");
        String idKey = MemberShip.getSessionKey("Id");
        String sessionIdKey = MemberShip.getSessionKey("SessionId");
        String userRoleIdsKey = MemberShip.getSessionKey("UserRoleIds");
        String name = null;
        if (session != null) {
            name = session.get(nameKey, String.class);
            id = session.get(idKey, Long.class);
            sessionId = session.get(sessionIdKey, String.class);
            userRoleIds = session.get(userRoleIdsKey, String.class);
        }
        if (!StringUtils.isEmpty(name)) {
            return new MemberShip(name,id,sessionId,userRoleIds);
        }
        return null;
    }
}